import re

log_line = '''"-" "-" "[28/Feb/2019:13:47:33 +0800]" "GET /center_h/latestActivities/list?pageNo=1&pageSize=5&type=1&time=1551332848925 HTTP/1.1" gxh.izhixue.org 200 1890 "http://gxh.izhixue.org/index.html" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" - 0.006 0.006 127.0.0.1:8080 200
'''


# log_line = '''121.193.175.253 - [28/Feb/2019:13:47:33 +0800] "GET /center_h/latestActivities/list?pageNo=1&pageSize=5&type=1&time=1551332848925 HTTP/1.1" gxh.izhixue.org 200 1890 "http://gxh.izhixue.org/index.html" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" - 0.006 0.006 127.0.0.1:8080 200'''


def main():
    # pattern_01 = r'''(?P<remote_addr>(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))'''
    pattern_01 = r'''"(?P<remote_addr>[^"]+)" "(?P<remote_user>[^"]+)" "(?P<time_local>[^"]+)"'''

    # pattern_01 = r'''(?P<remote_addr>^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - (?P<time_local>\[\d.*\])'''
    # pattern_01 = r'''(?P<remote_addr>^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - (?P<time_local>\[\d.*\]) (?P<method>\S+)'''
    # pattern_01 = r'''(?P<remote_addr>^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - (?P<time_local>\[\d.*\]) "(?P<request>[^"]+)"'''
    # pattern_01 = r'''(?P<remote_addr>^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - (?P<time_local>\[\d.*\]) "(?P<request>[^"]+)" (?P<http_host>\S+)'''
    # pattern_01 = r'''(?P<remote_addr>^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - (?P<time_local>\[\d.*\]) "(?P<request>[^"]+)" (?P<http_host>\S+) (?P<status>\d+) (?P<body_bytes_sent>\d+) "(?P<http_referer>[^"]+)" "(?P<http_user_agent>[^"]+)"'''
    # pattern_01 = r'''(?P<remote_addr>^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - (?P<time_local>\[\d.*\]) "(?P<request>[^"]+)" (?P<http_host>\S+) (?P<status>\d+) (?P<body_bytes_sent>\d+) "(?P<http_referer>[^"]+)" "(?P<http_user_agent>[^"]+)" (?P<http_x_forwarded_for>\S+)'''
    regex = re.compile(pattern_01)
    mo = regex.search(log_line)
    print(mo.groups())


if __name__ == '__main__':
    main()
